package com.lyb.spzx.order.controller;

import com.github.pagehelper.PageInfo;
import com.lyb.spzx.model.dto.h5.OrderInfoDto;
import com.lyb.spzx.model.entity.order.OrderInfo;
import com.lyb.spzx.model.vo.common.Result;
import com.lyb.spzx.model.vo.common.ResultCodeEnum;
import com.lyb.spzx.model.vo.h5.TradeVo;
import com.lyb.spzx.order.service.OrderInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "提交订单接口管理")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {

    @Autowired
    OrderInfoService orderInfoService;

    @Operation(summary = "提交下单")
    @GetMapping("/auth/trade")
    public Result<TradeVo> trade(){
        TradeVo tradeVo = orderInfoService.trade();
        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "提交订单")
    @PostMapping("/auth/submitOrder")
    public Result<Long> submitOrder(@RequestBody OrderInfoDto orderInfoDto){
        Long orderId = orderInfoService.submitOrder(orderInfoDto);
        return Result.build(orderId,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取订单信息")
    @GetMapping("/auth/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable Long orderId){
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderId);
        return Result.build(orderInfo,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "立即购买")
    @GetMapping("/auth/buy/{skuId}")
    public Result<TradeVo> buy(@PathVariable Long skuId){
        TradeVo tradeVo = orderInfoService.buy(skuId);
        return Result.build(tradeVo,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取订单分页列表")
    @GetMapping("/auth/{page}/{limit}")
    public Result<PageInfo<OrderInfo>> getAllTrade(@PathVariable Integer page,
                                                   @PathVariable Integer limit,
                                                   Integer orderStatus){
        PageInfo<OrderInfo> orderInfoPageInfo = orderInfoService.getAllTrade(page,limit,orderStatus);
        return Result.build(orderInfoPageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取订单信息")
    @GetMapping("/auth/getOrderInfoByOrderNo/{orderNo}")
    public Result<OrderInfo> getOrderInfoByOrderNo(@PathVariable String orderNo){
        OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
        return Result.build(orderInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "更新订单支付状态")
    @GetMapping("/auth/updateOrderStatusPayed/{orderNo}/{payType}")
    public Result updateOrderStatus(@PathVariable String orderNo, @PathVariable Integer payType){
        orderInfoService.updateOrderStatus(orderNo,payType);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
